<?php
/**
 * 商品缺货预定API文件
 * User: zhoujh
 * Email:449077794@qq.com
 * Date: 2016/11/02
 * Time: 09:16
 */

namespace SDXApi\Controller;

use SDXApi\Common\Tools\AppVersion;
use SDXApi\Service\CommunityNewService;
use Think\Controller;
use SDXApi\Service\UserBehavior;


/**
 * Class CommunityController
 * @author Jerry
 * @package SDXApi\Controller
 */
class CommunityController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        header("Access-Control-Allow-Origin: *");
    }

    //社区是否显示
    public function is_show()
    {
        $is_show = M("shop_config")->where(array('id' => 911))->getField("value");
        $app = AppVersion::getAppVersion();

        if ($app['app'] == 'h5') {
            returnApiSuccess(array('is_show' => $is_show));
        }

        returnApiSuccess(array('is_show' => 0));

    }

    //社区分类
    public function get_cat_list()
    {
        $community_cat = M("community_cat")->field("cat_name,cat_id")->where(array('parent_id' => 0, 'show_in_nav' => 1))->order("sort_order desc")->select();
        returnApiSuccess(array('list' => $community_cat), 'community classification');
    }

    public function time_tran($show_time)
    {
        $now_time = strtotime("now");
        $dur = $now_time - $show_time;
        if ($dur < 0) {
            return $the_time;
        } else {
            if ($dur < 60) {
                return $dur . '秒前';
            } else {
                if ($dur < 3600) {
                    return floor($dur / 60) . '分钟前';
                } else {
                    if ($dur < 86400) {
                        return floor($dur / 3600) . '小时前';
                    } else {
                        if ($dur < 259200) {//3天内
                            return floor($dur / 86400) . '天前';
                        } else {
                            return local_date("m-d", $show_time);
                        }
                    }
                }
            }
        }
    }

    /**
     * @author Jerry
     */
    public function get_author_article_list()
    {
        $author_id = I("post.author_id", 0, "int");
        $uid = I("post.uid", 0, "int");
        $current_page = I('post.page', 1);   //页码
        $start = ($current_page - 1) * 20;
        $page_current = !empty($current_page) ? $current_page : 1;

        $communityService = new CommunityNewService();
        $community_list = $communityService->getCommunityList($author_id, $uid, $current_page, $start, $page_current);

        returnApiSuccess($community_list, 'article list');

    }

    /**
     * 文章列表
     * @author Jerry
     */
    public function get_article_list()
    {
        $cat_id = I("post.cat_id", 0, "int");
        $uid = I("post.uid", 0, "int");
        $current_page = I('post.page', 1);   //页码

        $start = ($current_page - 1) * 20;
        $page_current = !empty($current_page) ? $current_page : 1;

        $communityService = new CommunityNewService();
        $community_list = $communityService->getArticleList($cat_id, $uid, $current_page, $start, $page_current);

        returnApiSuccess($community_list, 'article list');

    }

    /**
     * 文章详情
     * @author Jerry
     */
    public function get_article_detail()
    {
        $id = I("post.id", 0, "int");
        $uid = I("post.uid", 0, "int");
        $wid = I('post.wid', 0, 'int');

        if ($id == 0) {
            returnApiError('id is null');
        }

        $communityService = new CommunityNewService();
        $article = $communityService->getArticleDetail($id, $uid, $wid);

        if (! empty($article)) {
            $article['is_vip'] = $this->getUserRank($uid);
            $article['add_time'] = local_date("Y年m月d日", $article['add_time']);

            //添加行为
            $user_behavior = new UserBehavior();
            $user_behavior->add_community_browse($uid, 2, $id, $article['title']);

            returnApiSuccess($article);
            exit;
        }

        returnApiError('暂无数据');
        exit;
    }

    /**
     * @author Jerry
     * @param $userId
     * @return int
     */
    protected function getUserRank($userId)
    {

        $rank = D('Users')->get_user_rank($userId);

        if ($rank['user_rank'] == 2) {
            return 1;
        }

        return 0;
    }

//设置文章为喜欢
    public function set_like()
    {
        $id = I("post.id", 0, "int");
        $uid = I("post.uid", 0, "int");
        if ($id == 0) {
            returnApiError("id is null");
        }

        if ($uid == 0) {
            returnApiError("uid is null");
        }
        $user_behavior = new UserBehavior();
        $data = array('community_id' => $id, 'user_id' => $uid);
        $count = M("community_like")->where($data)->count();
        $info = M("community")->where(array('community_id' => $id))->field("author,title")->find();
        if ($count > 0) {
            $state = M("community_like")->where($data)->delete();
            if ($state !== false) {
                M("community")->where(array('community_id' => $id))->setInc('like_times', -1);
                M("community_author")->where(array('id' => $info['author']))->setInc('like_times', -1);
                $count = M("community")->where(array('community_id' => $id))->getField("like_times");

                //添加行为
                $user_behavior->add_community_browse($uid, 4, $id, $info['title']);

                returnApiSuccess(array("set" => "0", "count" => $count), 'delete like success');
            } else {
                returnApiError("fail");
            }
        } else {
            $state = M("community_like")->add($data);
            if ($state !== false) {
                M("community")->where(array('community_id' => $id))->setInc('like_times', 1);
                M("community_author")->where(array('id' => $info['author']))->setInc('like_times', 1);
                $count = M("community")->where(array('community_id' => $id))->getField("like_times");

                //添加行为
                $user_behavior->add_community_browse($uid, 3, $id, $info['title']);

                returnApiSuccess(array("set" => "1", "count" => $count), 'set like success');
            } else {
                returnApiError("fail");
            }
        }


    }

    //保存转发行为
    public function forward()
    {
        $id = I("post.id", 0, "int");
        $uid = I("post.uid", 0, "int");
        $pf = I("post.pf");
        if ($id == 0) {
            returnApiError("id is null");
        }
        if ($uid == 0) {
            returnApiError("uid is null");
        }
        $info = M("community")->where(array('community_id' => $id))->getField("author");
        $data = array('community_id' => $id, "user_id" => $uid, "platform" => $pf);
        $state = M("community_forward")->add($data);
        if ($state !== false) {
            M("community")->where(array('community_id' => $id))->setInc('forwarding_times', 1);
            M("community_author")->where(array('id' => $info['author']))->setInc('forwarding_times', 1);
        } else {
            returnApiError("fail");
        }

        returnApiSuccess(array('status' => 1), 'success');

    }


    /**
     * @author Jerry
     */
    public function get_collect_article()
    {
        $userId = I('post.uid', 0, 'int');

        if (empty($userId)) {
            returnApiError('用户id不能为空');
        }

        $communityService = new CommunityNewService();
        $list = $communityService->getCollectArticle($userId);

        if (empty($list)) {
            returnApiError('暂无数据');
        }

        $data = array(
            'collect_list'  =>  $list,
        );
        returnApiSuccess($data);
    }

    /**
     * 数据导入
     * @author Jerry
     * @return bool
     */
    protected function communityDataExport()
    {
        $community = D('community')->findCommunity();

        if (empty($community)) {
            return false;
        }

        foreach ($community as $key => $value) {
            $data = array(
                'title'         =>  '默认导入模块',
                'template_type' =>  1,
                'community_id'  =>  $value['community_id'],
            );

            $id = D('CommunityTemplate')->insertTemplate($data);
            $contentData = array(
                'content'       =>  $value['content'],
                'template_id'   =>  $id,
            );
            D('CommunityTemplateContent')->insertContent($contentData);
        }

        echo '数据导入完成';
    }

}